home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7317 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  58 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C constant expression declarations
  5. Date: 15 Feb 1996 20:45:10 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb15134510@qcd.lanl.gov>
  8. References: <31229735.41C67EA6@isi.com> <DMto56.Lo3@uns.bris.ac.uk>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: nathan@pact.srf.ac.uk's message of Thu, 15 Feb 1996 14:55:54 GMT
  13.  
  14. In article <DMto56.Lo3@uns.bris.ac.uk> nathan@pact.srf.ac.uk (Nathan
  15. Sidwell) writes: 
  16. <snip>
  17.    However, a gotcha is using signed constants, for example
  18.  
  19.    #define    EXPR -1
  20.    if(a EXPR) ...
  21.  
  22.    this will complile as 'a - 1', rather than give a syntax error
  23.  
  24.    leaving out the brackets, will not affect correct code, but can cause
  25.  
  26. #include <stdio.h>
  27. int main(void) {
  28.    printf("%d\n",EXPR["\3\2\1"+1])
  29.    return 0;
  30. }
  31.  
  32. :-)
  33.  
  34.    incorrect code to be compiled to something undesired, rather than give
  35.    an error. Having brackets o unsigned constants, just reinforces the habit.
  36.  
  37.    Even wih the brackets, I could still have silent error, for instance
  38.  
  39.    #define EXPR (-1)
  40.    int a(int);
  41.    if(a EXPR) ...
  42.  
  43.    will be compiled as 'a(-1)'. However this compounds two errors, using
  44.    a function name instead of a variable (I assume you meant to compare
  45.    a variable with a value), and omit the comparison operator.
  46.  
  47. Or maybe one error? Trying to test if a == (int(*)(int))0 + 1; ?
  48.  
  49. Cheers
  50. Tanmoy
  51. --
  52. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  53. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  54. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  55. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  56. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  57. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  58.